package com.my.study;

public class ThreadLocalStudy {


    private ThreadLocal<Integer> threadLocal = new ThreadLocal<>();


    public void start(){

        for(int i = 0;i < 20;i++){

            int a = i;
            new Thread(new Runnable() {
                @Override
                public void run() {

                    threadLocal.set(a);
                   // threadLocal.set(101);
                    System.out.println(Thread.currentThread().getName() + "     " + threadLocal.get());

                    //防止出现内存泄漏
                    threadLocal.remove();
                }
            }).start();
        }
    }



    public static void main(String[] args) {


        ThreadLocalStudy study = new ThreadLocalStudy();
        study.start();

    }
}
